﻿using GalaSoft.MvvmLight.Command;
using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core;
using Glorysoft.EAP.Core.Enums;
using Glorysoft.EAP.EAPClient.Common;
using Glorysoft.EAP.EAPClient.Model;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Windows;

namespace Glorysoft.EAP.EAPClient.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class UserLoginViewModel : PopupWindowVM
    {
        public static string userloginpassword;
        private readonly Logger ServiceLogger = XfwContext.EapLogger;
        public delegate void CloseDelegate();
        public event CloseDelegate CloseEvent;
        /// <summary>
        /// Initializes a new instance of the LoginViewModel class.
        /// </summary>
        string EQPPORT = string.Empty;
        public UserLoginViewModel()
        {
        }
        public Model.RunService ClientInfo
        {
            get
            {
                return Model.RunService.Current;
            }
        }
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("UserId");
            }
        }
        private string password;
        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                userloginpassword = value;
                RaisePropertyChanged("Password");
            }
        }
        #region command
        private RelayCommand<string> UserloginCmd;
        public RelayCommand<string> UserLoginCmd
        {
            get
            {
                return UserloginCmd ?? (UserloginCmd = new RelayCommand<string>(UserLogin));
            }
        }

        public void UserLogin(string obj)
        {
           
            try
            {
                if (string.IsNullOrEmpty(Name))
                {
                    MessageBox.Show(Consts.GetTranslation("Msg_UserIdInputInfo"), Consts.GetTranslation("Msg_ConfirmInfo"), MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(Password))
                {
                    MessageBox.Show(Consts.GetTranslation("Msg_UserPwdInputInfo"), Consts.GetTranslation("Msg_ConfirmInfo"), MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                var FacilityId = ClientInfo.EQPInfo.FacilityId;

                Client2Login(Name.Trim().ToUpper(), Password, FacilityId);
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
        }
        public static Dictionary<string, string> userLogin = new Dictionary<string, string>();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="password"></param>
        /// <param name="facilityId"></param>
        public void Client2Login(string Name, string password, string facilityId)
        {
            try
            {
                string eqpid = EQPStatus.currenteqpid;
                if (userLogin.ContainsKey(eqpid))
                {
                    userLogin[eqpid] = Name;
                }
                else
                {
                    userLogin.Add(eqpid, Name);
                }
                EAPClient client = new EAPClient();
                var _return = client.Login(Name, password, facilityId);
                if (_return != null && _return.ReturnCode == "OK")
                {
                    this.Close();
                    ClientInfo.EQPInfo.LotList[0].UserId = Name;
                    ClientInfo.EQPInfo.BtnOk = Visibility.Visible;
                    ClientInfo.EQPInfo.Btnclean = Visibility.Visible;
                    ClientInfo.EQPInfo.Loginout = Visibility.Visible;            
                }
                else
                {
                    CMessageBox.Show(Consts.GetTranslation("Msg_Loginfail"));              
                }
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
        }
        private RelayCommand cancelCmd;
        public RelayCommand CancelCmd
        {
            get { return cancelCmd ?? (cancelCmd = new RelayCommand(Cancel)); }
        }
        public void Cancel()
        {
            try
            {
                if (CloseEvent != null)
                    CloseEvent();
                ClientInfo.EQPInfo.Loginin = Visibility.Visible;
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
            this.Close();
        }
        #endregion
    }
}